namespace com.stifel.ServerBuildMgmt.Features.ServerBuildMgmtWebparts
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Xml;

    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "6716846e-5630-42d8-a7c1-1059244b5d02" )]
    public class ServerBuildMgmtWebpartsEventReceiver : SPFeatureReceiver
    {
        #region Fields

        public const string FEATURE = "com.stifel.ServerBuildMgmt";

        const string ADGroupName = "AD." + GROUPTYPE;
        const string adpage = "adpage" + pageType;
        const string adteam = "adteam";
        const string AntiVirusGroupName = "AntiVirus." + GROUPTYPE;
        const string ApprovalGroupName = "Approval." + GROUPTYPE;
        const string approvalpage = "approvalpage" + pageType;
        const string approvalteam = "approvalteam";
        const string avpage = "avpage" + pageType;
        const string avteam = "avteam";
        const string BackupGroupName = "Backup." + GROUPTYPE;
        const string backuppage = "backuppage" + pageType;
        const string backupteam = "backupteam";
        const string BuildGroupName = "Build." + GROUPTYPE;
        const string buildpage = "buildpage" + pageType;
        const string buildteam = "buildteam";
        const string CONFIGCONTENTTYPEGROUP = "Stifel Configuration";
        const string GROUPTYPE = "Member";
        const string initialrequestpage = "initialrequestpage" + pageType;
        const string NetworkMonitorGroupName = "NetworkMonitor."+GROUPTYPE;
        const string networkmonitorteam = "networkmonitorteam";
        const string networkpage = "networkpage" + pageType;
        const string NotificationTemplate = "Request @requestid is available for review.To review go to @requesturl.";
        const string NotificationTitle = "Request @requestid";
        const string pageType = ".aspx";
        const string PatchGroupName = "Patch." + GROUPTYPE;
        const string patchpage = "patchpage" + pageType;
        const string patchteam = "patchteam";
        const string projects = "projects";
        const string ReqAccNotificationTemplate = "Request @requestid is Accepted. To review go to @requesturl.";
        const string ReqAccNotificationTitle = "Request @requestid : Request Accepted";
        const string ReqComNotificationTemplate = "Request @requestid is Completed. To review go to @requesturl.";
        const string ReqComNotificationTitle = "Request @requestid : Request Completed";
        const string ReqRejNotificationTemplate = "Request @requestid is rejected. To review go to @requesturl.";
        const string ReqRejNotificationTitle = "Request @requestid : Request Rejected";
        const string SitePages = "SitePages";
        const string virtualserverbuildrqs = "virtualserverbuildrqs";

        List<string> fields1 = new string[ ] {"Stifel-Configuration.xml" }.ToList( );

        //List<string> fields1 = new string[ ] { "Stifel-Notification.xml", "Stifel-Configuration.xml" }.ToList( );
        List<ContentTypeDefn> _ContentTypes2;
        List<ListDefn> _Lists2;

        #endregion Fields

        #region Properties

        List<ContentTypeDefn> ContentTypes2
        {
            get
            {
                if ( _ContentTypes2 == null )
                {
                    _ContentTypes2 = new List<ContentTypeDefn>( );
                    {
                        StifelVirtualServerRequestConfiguration c = null;
                        _ContentTypes2.Add( new ContentTypeDefn
                        {
                            Name = Item.GetContentTypeName<StifelVirtualServerRequestConfiguration>( ),
                            ContentTypeId = Item.GetContentTypeId<StifelVirtualServerRequestConfiguration>( ),
                            Group = CONFIGCONTENTTYPEGROUP,
                            Description = "Defines the configuration options for the virtual server build request.",
                            Fields = new FieldDefn[ ] {
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.Active))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADSecurityGroupId)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADSendEmail))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADSiteURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalSecurityGroupId)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalSendEmail))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalSiteURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVSecurityGroupId)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVSendEmail))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVSiteURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupSecurityGroupId)),
                                    Required=true
                                },
                                new FieldDefn{ Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupSendEmail))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupSiteURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildSecurityGroupId)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildSendEmail))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildSiteURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorSecurityGroupId)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorSendEmail))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorSiteURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchSecurityGroupId)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchSendEmail))
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchSiteURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestAcceptedNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestAcceptedNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestRejectedNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestRejectedNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestorPageURL)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestCompletionNotificationTitle)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestCompletedNotificationTemplate)),
                                    Required=true
                                },
                                new FieldDefn{
                                    Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestSiteURL)),
                                    Required=true
                                },
                            }.Distinct( ).ToList( )
                        } );
                    }
                }
                return _ContentTypes2;
            }
        }

        //List<ContentTypeDefn> ContentTypes2
        //{
        //    get
        //    {
        //        if ( _ContentTypes2 == null )
        //        {
        //            _ContentTypes2 = new List<ContentTypeDefn>( );
        //            {
        //                StifelNotification c = null;
        //                _ContentTypes2.Add( new ContentTypeDefn
        //                {
        //                    Name = Item.GetContentTypeName<StifelNotification>( ),
        //                    ContentTypeId = Item.GetContentTypeId<StifelNotification>( ),
        //                    Group = CONTENTTYPEGROUP,
        //                    Description = "Defines a notification.",
        //                    Fields = new FieldDefn[ ] {
        //                        new FieldDefn{ Name=Item.GetColumnName<StifelNotification>(c.GetPropertySymbol(o=>o.Notification))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelNotification>(c.GetPropertySymbol(o=>o.NotificationURL))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelNotification>(c.GetPropertySymbol(o=>o.RequestIdentifier))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelNotification>(c.GetPropertySymbol(o=>o.RequestUniqueId))
        //                        }
        //                    }.Distinct( ).ToList( )
        //                } );
        //            }
        //            {
        //                StifelVirtualServerRequestConfiguration c = null;
        //                _ContentTypes2.Add( new ContentTypeDefn
        //                {
        //                    Name = Item.GetContentTypeName<StifelVirtualServerRequestConfiguration>( ),
        //                    ContentTypeId = Item.GetContentTypeId<StifelVirtualServerRequestConfiguration>( ),
        //                    Group = CONFIGCONTENTTYPEGROUP,
        //                    Description = "Defines the configuration options for the virtual server build request.",
        //                    Fields = new FieldDefn[ ] {
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.Active))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADSecurityGroupId)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADSendEmail))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ADSiteURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalSecurityGroupId)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalSendEmail))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.ApprovalSiteURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVSecurityGroupId)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVSendEmail))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.AVSiteURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupSecurityGroupId)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{ Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupSendEmail))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BackupSiteURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildSecurityGroupId)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildSendEmail))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.BuildSiteURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorSecurityGroupId)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorSendEmail))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.MonitorSiteURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchSecurityGroupId)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchSendEmail))
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.PatchSiteURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestAcceptedNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestAcceptedNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestRejectedNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestRejectedNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestorPageURL)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestCompletionNotificationTitle)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestCompletedNotificationTemplate)),
        //                            Required=true
        //                        },
        //                        new FieldDefn{
        //                            Name=Item.GetColumnName<StifelVirtualServerRequestConfiguration>(c.GetPropertySymbol(o=>o.RequestSiteURL)),
        //                            Required=true
        //                        },
        //                    }.Distinct( ).ToList( )
        //                } );
        //            }
        //        }
        //        return _ContentTypes2;
        //    }
        //}
        List<ListDefn> Lists2
        {
            get
            {
                if ( _Lists2 == null )
                {
                    _Lists2 = new List<ListDefn>( );
                    _Lists2.Add( new ListDefn
                    {
                        Title = "Stifel Virtual Server Request Configuration",
                        RootFolderName = "stifelvirtualserverrequestconfiguration",
                        Description = "This contains the virtual server request configuration options.",
                        ContentTypes = new string[ ] {
                            Item.GetContentTypeName<StifelVirtualServerRequestConfiguration>()
                        }.ToList( )
                    } );
                }
                return _Lists2;
            }
        }

        #endregion Properties

        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPSite ) )
                {
                    throw new Exception( string.Format( "{0} :: Error activating feature. Feature parent is not of type SPSite.", FEATURE ) );
                }

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature activating.", FEATURE );
                addFields( properties, fields1 );
                createContentTypes( properties, ContentTypes2 );
                createLists( properties, Lists2 );
                addItems( properties );
                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature activated.", FEATURE );
            }
            catch ( Exception e )
            {
                throw new Exception( string.Format( "Error occurred in {0} FeatureActivated. {1} {2}", FEATURE, e.Message, e.StackTrace ), e );
            }
        }

        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            try
            {
                if ( !( properties.Feature.Parent is SPSite ) )
                {
                    CustomLoggingService.LogCritical( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Error deactivating feature. Feature parent is not of type SPSite.", FEATURE );
                    return;
                }

                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature deactivating.", FEATURE );
                deleteItems( properties );
                deleteLists( properties, Lists2 );
                deleteContentTypes( properties, ContentTypes2 );
                removeFields( properties, fields1 );
                CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: Feature deactivated." );
            }
            catch ( Exception e )
            {
                throw new Exception( string.Format( "Error occurred in {0} FeatureDeactivating. {1} {2}", FEATURE, e.Message, e.StackTrace ), e );
            }
        }

        public SPGroup getUserGroup( SPFeatureReceiverProperties properties, string team, string groupName )
        {
            SPGroup group;
            string url = properties.GetSite( ).Url + "/" + team;
            SPSite spsite = new SPSite( url );
            using ( SPWeb spWeb = spsite.OpenWeb( ) )
            {
                group = spWeb.Groups[ groupName ];
            }
            return group;
        }

        void addFields( SPFeatureReceiverProperties properties, List<string> fieldResource )
        {
            foreach ( string resource in fieldResource )
            {
                XmlNodeList fields = getFieldDefinitionsAsXml( resource );
                if ( fields != null )
                {
                    using ( SPWeb spWeb = properties.GetWeb( ) )
                    {
                        foreach ( XmlNode field in fields )
                        {
                            XmlAttribute a = field.Attributes[ "Name" ];
                            if ( a != null )
                            {
                                string name = a.Value;
                                if ( !spWeb.Fields.ContainsFieldWithStaticName( name ) )
                                {
                                    bool ok = true;
                                    string s = field.OuterXml;
                                    const string LISTMARKER = "List=\"$";
                                    if ( s.Contains( LISTMARKER ) )
                                    {
                                        int x1 = s.IndexOf( LISTMARKER ) + LISTMARKER.Length;
                                        int x2 = s.IndexOf( "$\"", x1 );
                                        string listurl = s.Substring( x1, x2 - x1 );
                                        if ( !string.IsNullOrEmpty( listurl ) )
                                        {
                                            SPList spList = spWeb.Lists.GetListByRootFolder( listurl );
                                            if ( spList != null )
                                                s = s.Replace( string.Format( "\"${0}$\"", listurl ), string.Format( "\"{0}\"", spList.ID.ToString( "B" ) ) );
                                            else
                                                ok = false;
                                        }
                                    }
                                    if ( ok )
                                    {
                                        CustomLoggingService.LogMessage( CustomLoggingService.Area.Fields, s );
                                        spWeb.Fields.AddFieldAsXml( s );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        void addItems( SPFeatureReceiverProperties properties )
        {
            string siteUrl = properties.GetSite( ).Url + "/" + projects + "/";
            string adPageUrl = siteUrl + adteam + "/" +  SitePages + "/";
            string avPageUrl = siteUrl + avteam + "/" + SitePages + "/";
            string approvalPageUrl = siteUrl + approvalteam + "/" + SitePages + "/";
            string backupPageUrl = siteUrl + backupteam + "/" + SitePages + "/";
            string buildPageUrl = siteUrl + buildteam + "/" + SitePages + "/";
            string moniterPageUrl = siteUrl + networkmonitorteam + "/" + SitePages + "/";
            string patchPageUrl = siteUrl + patchteam + "/" + SitePages + "/";
            string reqSiteUrl = properties.GetSite( ).Url + "/";
            string reqPageUrl = reqSiteUrl + virtualserverbuildrqs + "/" + SitePages + "/";
            StifelVirtualServerRequestConfigurationDao dao = new StifelVirtualServerRequestConfigurationDao( );
            dao.Clear( );
            dao.Add( new StifelVirtualServerRequestConfiguration
            {
                Title = "Configuration",
                Active = true,
                ADNotificationTemplate = NotificationTemplate,
                ADNotificationTitle = NotificationTitle,
                ADPageURL = adPageUrl + adpage,
                ADSecurityGroupId = getUserGroup(properties,adteam,ADGroupName).ID,
                ADSendEmail = false,
                ADSiteURL = siteUrl + adteam,
                AVNotificationTemplate = NotificationTemplate,
                AVNotificationTitle = NotificationTitle,
                AVPageURL = avPageUrl + avpage,
                AVSecurityGroupId = getUserGroup(properties,avteam,AntiVirusGroupName).ID,
                AVSendEmail = false,
                AVSiteURL = siteUrl + avteam,
                ApprovalNotificationTemplate = NotificationTemplate,
                ApprovalNotificationTitle = NotificationTitle,
                ApprovalPageURL = approvalPageUrl + approvalpage,
                ApprovalSecurityGroupId = getUserGroup(properties,approvalteam,ApprovalGroupName).ID,
                ApprovalSendEmail = false,
                ApprovalSiteURL = siteUrl + approvalteam,
                BackupNotificationTemplate = NotificationTemplate,
                BackupNotificationTitle = NotificationTitle,
                BackupPageURL = backupPageUrl + backuppage,
                BackupSecurityGroupId = getUserGroup(properties,backupteam,BackupGroupName).ID,
                BackupSendEmail = false,
                BackupSiteURL = siteUrl + backupteam,
                BuildNotificationTemplate = NotificationTemplate,
                BuildNotificationTitle = NotificationTitle,
                BuildPageURL = buildPageUrl + buildpage,
                BuildSecurityGroupId = getUserGroup(properties,buildteam,BuildGroupName).ID,
                BuildSendEmail = false,
                BuildSiteURL = siteUrl + buildteam,
                MonitorNotificationTemplate = NotificationTemplate,
                MonitorNotificationTitle = NotificationTitle,
                MonitorPageURL = moniterPageUrl + networkpage,
                MonitorSecurityGroupId = getUserGroup(properties,networkmonitorteam,NetworkMonitorGroupName).ID,
                MonitorSendEmail = false,
                MonitorSiteURL = siteUrl + networkmonitorteam,
                PatchNotificationTemplate = NotificationTemplate,
                PatchNotificationTitle = NotificationTitle,
                PatchPageURL = patchPageUrl + patchpage,
                PatchSecurityGroupId = getUserGroup(properties,patchteam,PatchGroupName).ID,
                PatchSendEmail = false,
                PatchSiteURL = siteUrl + patchteam,
                RequestAcceptedNotificationTemplate = ReqAccNotificationTemplate,
                RequestAcceptedNotificationTitle = ReqAccNotificationTitle,
                RequestCompletedNotificationTemplate = ReqComNotificationTemplate,
                RequestCompletionNotificationTitle = ReqComNotificationTitle,
                RequestNotificationTemplate = NotificationTemplate,
                RequestNotificationTitle = NotificationTitle,
                RequestorPageURL = reqPageUrl + initialrequestpage,
                RequestRejectedNotificationTemplate = ReqRejNotificationTemplate,
                RequestRejectedNotificationTitle = ReqRejNotificationTitle,
                RequestSiteURL = reqSiteUrl + virtualserverbuildrqs,
            } );
        }

        void createContentTypes( SPFeatureReceiverProperties properties, List<ContentTypeDefn> contenttypes )
        {
            using ( SPWeb spWeb = properties.GetWeb( ) )
            {
                foreach ( ContentTypeDefn o in contenttypes )
                {
                    if ( !spWeb.ContainsContentType( o.Name ) )
                    {
                        List<SPField> fields = new List<SPField>( );
                        foreach ( FieldDefn f in o.Fields )
                        {
                            if ( f != null && !string.IsNullOrEmpty( f.Name ) )
                            {
                                SPField field = spWeb.Fields.GetFieldByInternalName( f.Name );
                                if ( field != null )
                                {
                                    field.Required = f.Required;
                                    field.Hidden = f.Hidden;
                                    fields.Add( field );
                                }
                            }
                        }
                        spWeb.CreateContentType( o.ContentTypeId, o.Group, o.Name, o.Description, false, fields.ToArray( ) );
                    }
                }
            }
        }

        void createLists( SPFeatureReceiverProperties properties, List<ListDefn> lists )
        {
            using ( SPWeb spWeb = properties.GetWeb( ) )
            {
                foreach ( ListDefn o in lists )
                {
                    if ( spWeb.Lists.ContainsList( o.RootFolderName ) )
                    {
                        CustomLoggingService.LogWarning( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: List {1} not created. List already exists.", FEATURE, o.RootFolderName );
                    }
                    else
                    {
                        SPList spList = spWeb.CreateGenericList( o.RootFolderName, o.Title, o.Description, false );

                        spList.ContentTypesEnabled = true;

                        foreach ( SPContentType p in spList.ContentTypes )
                        {
                            p.Hidden = true;
                        }

                        foreach ( string s in o.ContentTypes )
                        {
                            SPContentType p = spWeb.GetContentTypeByName( s );
                            if ( p != null )
                                spList.ContentTypes.Add( p );
                        }
                        spList.Update( );

                        CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: List {1} created.", FEATURE, o.RootFolderName );
                    }
                }
            }
        }

        void deleteContentTypes( SPFeatureReceiverProperties properties, List<ContentTypeDefn> contenttypes )
        {
            using ( SPWeb spWeb = properties.GetWeb( ) )
            {
                foreach ( ContentTypeDefn o in contenttypes )
                    spWeb.DeleteContentType( o.Name );
            }
        }

        void deleteItems( SPFeatureReceiverProperties properties )
        {
            StifelVirtualServerRequestConfigurationDao dao = new StifelVirtualServerRequestConfigurationDao( );
            dao.Clear( );
        }

        void deleteLists( SPFeatureReceiverProperties properties, List<ListDefn> lists )
        {
            using ( SPWeb spWeb = properties.GetWeb( ) )
            {
                foreach ( ListDefn list in lists )
                {
                    bool ok = spWeb.DeleteList( list.RootFolderName, false );
                    if ( ok )
                        CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: List {1} removed.", FEATURE, list.RootFolderName );
                    else
                        CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure, "{0} :: List {1} not removed.", FEATURE, list.RootFolderName );
                }
            }
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        void removeFields( SPFeatureReceiverProperties properties, List<string> fieldResource )
        {
            foreach ( string resource in fieldResource )
            {
                XmlNodeList fields = getFieldDefinitionsAsXml( resource );
                if ( fields != null )
                {
                    using ( SPWeb spWeb = properties.GetWeb( ) )
                    {
                        foreach ( XmlNode field in fields )
                        {
                            XmlAttribute a = field.Attributes[ "Name" ];
                            if ( a != null )
                            {
                                string name = a.Value;
                                if ( spWeb.Fields.ContainsFieldWithStaticName( name ) )
                                {
                                    SPField f = spWeb.Fields.GetFieldByInternalName( name );
                                    if ( !f.UsedInWebContentTypes )
                                        spWeb.Fields.Delete( name );
                                }
                            }
                        }
                    }
                }
            }
        }

        #endregion Methods

        #region Other

        // Uncomment the method below to handle the event raised after a feature has been installed.
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised before a feature is uninstalled.
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised when a feature is upgrading.
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        #endregion Other
    }
}